<?php

/************************************

EasyCB, Community Forum Software
Copyright (C) 2007  Jonathon D. Keogh <jonathon.keogh@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

$Id: page.admin.config.php 107 2007-09-05 14:29:48Z jonathon.keogh $

************************************/

require "inc.func.php";

if(!user_loggedin() || user_level(user_name(sess_get("UserID"))) < 2)
{
	page_redirect("page.boards.php");
}

page_header("Reset Statistics");

if(isset($_POST['continue']))
{
	page_section();
?>

Please wait...

<?php
	page_section();
?>

<script language="javascript" type="text/javascript">
<!--

setTimeout("window.location='?continue=true'", 1000);

//-->
</script>

<?php
} elseif(isset($_POST['cancel'])) {
	page_redirect("page.admin.php");
} elseif(isset($_GET['done'])) {
	page_section("Complete");
?>

The statistics have been re-generated.

<?php
	page_section();
} elseif(isset($_GET['continue'])) {
	config_set("UserCount", db_num_rows(db_query("SELECT * FROM `" . config_get("db_prefix") . "users` WHERE `Deleted`=0;")));
	config_set("PostCount", db_num_rows(db_query("SELECT * FROM `" . config_get("db_prefix") . "posts` WHERE `Deleted`=0;")));
	config_set("TopicCount", db_num_rows(db_query("SELECT * FROM `" . config_get("db_prefix") . "topics`;")));
	
	db_query("DELETE FROM `" . config_get("db_prefix") . "sessions` WHERE `LastTime` < " . (time() - config_get("SessionExpiresAfter")) . ";");
	
	$r = db_query("SELECT `ID`, `PostCount` FROM `" . config_get("db_prefix") . "users`;");
	while($row = db_fetch_assoc($r))
	{
		$posts = db_query("SELECT * FROM `" . config_get("db_prefix") . "posts` WHERE `Deleted`=0 AND `UserID`='" . db_escape($row['ID']) . "';");
		$posts = db_num_rows($posts);
		if($row['PostCount'] != $posts)
		{
			db_query("UPDATE `" . config_get("db_prefix") . "users` SET `PostCount`='" . db_escape($posts) . "' WHERE `ID`='" . db_escape($row['ID']) . "';");
		}
	}
	
	page_redirect("?done=true");
} else {
	page_section("Warning");
?>

<form action="?" method="POST">
	This process can take a long time to complete.<br>
	<br>
	<button type="submit" name="continue">Continue anyway</button> <button type="submit" name="cancel">Cancel</button>
</form>

<?php
	page_section();
}

page_footer('$Id: page.admin.config.php 107 2007-09-05 14:29:48Z jonathon.keogh $');

?>